package fcl.pattern;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;


import fcl.mylife.Itinerary;
import fcl.mylife.Move;
import fcl.mylife.Stop;




public class DataProcessing {

	public ArrayList<Double> busdis = new ArrayList<Double>();
	public ArrayList<Double> traindis = new ArrayList<Double>();
	public ArrayList<Double> cardis = new ArrayList<Double>();
	public ArrayList<Double> walkdis = new ArrayList<Double>();
	
	public ArrayList<Double> totalDis = new ArrayList<Double>();

	
	public ArrayList<Double> busCarbon = new ArrayList<Double>();
	public ArrayList<Double> trainCarbon = new ArrayList<Double>();
	public ArrayList<Double> carCarbon = new ArrayList<Double>();
	public ArrayList<Double> walkCarbon = new ArrayList<Double>();

	public ArrayList<Double> totalCarbon = new ArrayList<Double>();

	public Itinerary trajectory = new Itinerary();
	
	//set the maximum value here
    
	public double maximumDis;
	public double maximumCarbon;
	public double startTime;
	public double endTime;


	public DataProcessing(){
		
		
		this.getDataFromFile();
		this.calculation();
		
		//this is for testing
		//this.testing();
	}
	
	public void getDataFromFile(){
		
		trajectory.getPath();
		
	}

	
    public void calculation(){
    	
    	ArrayList<Move> trips= null;
    	
       	if(trajectory.getMoves()!=null)
       	{
       		trips = trajectory.getMoves();
       		
       	}
       	
       	//we have to clean everything before we recalculate
       	cleanArray();
       	//you can write your analysis here
       	
       	
       int tripNo = trips.size();

      
       Move myRecordS = trips.get(0);
       double startMilli = myRecordS.getStartTimeInLong();
       
       Move myRecordE = trips.get(tripNo-1);
       double endMilli = myRecordE.getStartTimeInLong();
       
       //Date dt = new Date();
       //double mMilli = dt.getTime();
       
       this.setmyStartTime(startMilli);
       this.setmyEndTime(endMilli);
       
       double oneSeventh = (endMilli - startMilli) / 7;
       
       double trainDis1 = 0;
       double carDis1 = 0;
       double busDis1 = 0;
       double walkDis1 = 0;
       
       double trainDis2 = 0;
       double carDis2 = 0;
       double busDis2 = 0;
       double walkDis2 = 0;
       
       double trainDis3 = 0;
       double carDis3 = 0;
       double busDis3 = 0;
       double walkDis3 = 0;
       
       double trainDis4 = 0;
       double carDis4 = 0;
       double busDis4 = 0;
       double walkDis4 = 0;
       
       double trainDis5 = 0;
       double carDis5 = 0;
       double busDis5 = 0;
       double walkDis5 = 0;
       
       double trainDis6 = 0;
       double carDis6 = 0;
       double busDis6 = 0;
       double walkDis6 = 0;
       
       double trainDis7 = 0;
       double carDis7 = 0;
       double busDis7 = 0;
       double walkDis7 = 0;
       
       double [] maxArr;
       maxArr = new double[tripNo-1];
       double maxDis = 0;
       
       
       
       for (int i=0 ; i< tripNo-1 ; i++){
    	   Move myRecord = trips.get(i);
           double curMilli = myRecord.getStartTimeInLong();
           double vehiDis = myRecord.getDis();
       
           if (startMilli < curMilli && curMilli < startMilli + oneSeventh) {
        	   int vType01 = myRecord.getVehicle();
        	   switch (vType01) {
        	   case 1:
        		   trainDis1 = trainDis1 + vehiDis; 
        		   maxArr[i] = trainDis1;         		   
        		   break;

        	   case 2:
        		   carDis1 = carDis1 + vehiDis;  
        		   maxArr[i] = carDis1; 
            	   break;

        	   case 3:
        		   busDis1 = busDis1 + vehiDis;
        		   maxArr[i] = busDis1;
            	   break;

        	   case 4 :
        		   walkDis1 = walkDis1 + vehiDis;
        		   maxArr[i] = walkDis1;
            	   break;
            	   
        	   case 5 :
        		   walkDis1 = walkDis1 + vehiDis;
        		   maxArr[i] = walkDis1;
            	   break;
        	   
        	   default:
        	   }
           }

           if (curMilli > startMilli + oneSeventh && curMilli < startMilli + (oneSeventh*2)) {
        	   int vType02 = myRecord.getVehicle();
        	   switch (vType02) {
        	   case 1:
        		   trainDis2 = trainDis2 + vehiDis;
        		   maxArr[i] = trainDis2;
        		   break;

        	   case 2:
        		   carDis2 = carDis2 + vehiDis; 
        		   maxArr[i] = carDis2;
            	   break;

        	   case 3:
        		   busDis2 = busDis2 + vehiDis;
        		   maxArr[i] = busDis2;
            	   break;

        	   case 4 :
        		   walkDis2 = walkDis2 + vehiDis;
        		   maxArr[i] = walkDis2;
            	   break;
            	   
        	   case 5 :
        		   walkDis2 = walkDis2 + vehiDis;
        		   maxArr[i] = walkDis2;
            	   break;
        	   
        	   default:
        	   }
           }

           if (curMilli > startMilli + (oneSeventh*2) && curMilli < startMilli + (oneSeventh*3)) {
        	   int vType03 = myRecord.getVehicle();
        	   switch (vType03) {
        	   case 1:
        		   trainDis3 = trainDis3 + vehiDis;
        		   maxArr[i] = trainDis3;
        		   break;

        	   case 2:
        		   carDis3 = carDis3 + vehiDis; 
        		   maxArr[i] = carDis3;
            	   break;

        	   case 3:
        		   busDis3 = busDis3 + vehiDis;
        		   maxArr[i] = busDis3;
            	   break;

        	   case 4 :
        		   walkDis3 = walkDis3 + vehiDis;
        		   maxArr[i] = walkDis3;
            	   break;
            	   
        	   case 5 :
        		   walkDis3 = walkDis3 + vehiDis;
        		   maxArr[i] = walkDis3;
            	   break;
        	   
        	   default:
        	   }
           }
    	   
           if (curMilli > startMilli + (oneSeventh*3) && curMilli < startMilli + (oneSeventh*4)) {
        	   int vType04 = myRecord.getVehicle();
        	   switch (vType04) {
        	   case 1:
        		   trainDis4 = trainDis4 + vehiDis;
        		   maxArr[i] = trainDis4;
        		   break;
        	   case 2:
        		   carDis4 = carDis4 + vehiDis;     
        		   maxArr[i] = carDis4;
            	   break;

        	   case 3:
        		   busDis4 = busDis4 + vehiDis;
        		   maxArr[i] = busDis4;
            	   break;

        	   case 4 :
        		   walkDis4 = walkDis4 + vehiDis;
        		   maxArr[i] = walkDis4;
            	   break;
            	   
        	   case 5 :
        		   walkDis4 = walkDis4 + vehiDis;
        		   maxArr[i] = walkDis4;
            	   break;
        	   
        	   default:
        	   }
           }
    	   
           if (curMilli > startMilli + (oneSeventh*4) && curMilli < startMilli + (oneSeventh*5)) {
        	   int vType05 = myRecord.getVehicle();
        	   switch (vType05) {
        	   case 1:
        		   trainDis5 = trainDis5 + vehiDis;
        		   maxArr[i] = trainDis5;
        		   break;

        	   case 2:
        		   carDis5 = carDis5 + vehiDis;  
        		   maxArr[i] = carDis5;
            	   break;

        	   case 3:
        		   busDis5 = busDis5 + vehiDis;
        		   maxArr[i] = busDis5;
            	   break;

        	   case 4 :
        		   walkDis5 = walkDis5 + vehiDis;
        		   maxArr[i] = walkDis5;
            	   break;
            	   
        	   case 5 :
        		   walkDis5 = walkDis5 + vehiDis;
        		   maxArr[i] = walkDis5;
            	   break;
        	   
        	   default:
        	   }
           }
    	   
           if (curMilli > startMilli + (oneSeventh*5) && curMilli < startMilli + (oneSeventh*6)) {
        	   int vType06 = myRecord.getVehicle();
        	   switch (vType06) {
        	   case 1:
        		   trainDis6 = trainDis6 + vehiDis;
        		   maxArr[i] = trainDis6;
        		   break;

        	   case 2:
        		   carDis6 = carDis6 + vehiDis;    
        		   maxArr[i] = carDis6;
            	   break;

        	   case 3:
        		   busDis6 = busDis6 + vehiDis;
        		   maxArr[i] = busDis6;
            	   break;

        	   case 4 :
        		   walkDis6 = walkDis6 + vehiDis;
        		   maxArr[i] = walkDis6;
            	   break;
            	   
        	   case 5 :
        		   walkDis6 = walkDis6 + vehiDis;
        		   maxArr[i] = walkDis6;
            	   break;
        	   
        	   default:
        	   }
           }

    	   
           if (curMilli > startMilli + (oneSeventh*6) && curMilli < endMilli) {
        	   int vType07 = myRecord.getVehicle();
        	   switch (vType07) {
        	   case 1:
        		   trainDis7 = trainDis7 + vehiDis;
        		   maxArr[i] = trainDis7;
        		   break;

        	   case 2:
        		   carDis7 = carDis7 + vehiDis; 
        		   maxArr[i] = carDis7;
            	   break;

        	   case 3:
        		   busDis7 = busDis7 + vehiDis;
        		   maxArr[i] = busDis7;
            	   break;

        	   case 4 :
        		   walkDis7 = walkDis7 + vehiDis;
        		   maxArr[i] = walkDis7;
            	   break;
            	   
        	   case 5 :
        		   walkDis7 = walkDis7 + vehiDis;
        		   maxArr[i] = walkDis7;
            	   break;
        	   
        	   default:
        	   }
           }
           
       }
       
       for (int j=0 ; j<tripNo-1 ; j++){
    	   if ( maxDis < maxArr[j]) {
    		   maxDis = maxArr[j];
    	   }
       }
       
       this.setMaxDis(maxDis);
       
       this.traindis.add(trainDis1);  
	   this.cardis.add(carDis1);
	   this.busdis.add(busDis1);
	   this.walkdis.add(walkDis1);
       
       this.traindis.add(trainDis2); 
	   this.cardis.add(carDis2);
	   this.busdis.add(busDis2);
	   this.walkdis.add(walkDis2);
	   
       this.traindis.add(trainDis3); 
	   this.cardis.add(carDis3);
	   this.busdis.add(busDis3);
	   this.walkdis.add(walkDis3);
       
       this.traindis.add(trainDis4); 
	   this.cardis.add(carDis4);
	   this.busdis.add(busDis4);
	   this.walkdis.add(walkDis4);
	   
       this.traindis.add(trainDis5);
	   this.cardis.add(carDis5);
	   this.busdis.add(busDis5);
	   this.walkdis.add(walkDis5);
       
       this.traindis.add(trainDis6); 
	   this.cardis.add(carDis6);
	   this.busdis.add(busDis6);
	   this.walkdis.add(walkDis6);
	   
       this.traindis.add(trainDis7);
	   this.cardis.add(carDis7);
	   this.busdis.add(busDis7);
	   this.walkdis.add(walkDis7);
       
       
       /*
       int mYear = dt.getYear();
       int mMonth = dt.getMonth();
       int mDay = dt.getDay();
       
       for (int i=0 ; i< tripNo ; i++){
       			
       	Move myRecord = trips.get(i);
       	Date starttime = myRecord.getStartTime();
       	int RecYear = starttime.getYear();
       	int RecMonth = starttime.getMonth();
       	int RecDay = starttime.getDay();
       	int vType = myRecord.getVehicle();
       
       	if ( RecYear == mYear  && RecMonth == mMonth && RecDay == mDay ) {
       		
       		if (RecYear == mYear  && RecMonth == mMonth && RecDay == (mDay - n ) ) {
       		
       		case 
       		
       	}
       	
       	double myDis = myRecord.getDis();
       	double myStartX = myRecord.getStart().getX();
       	Stop myEnd = myRecord.getEnd();
       	int vType = myRecord.getVehicle();
       	
       	}
       	*/
       	
       	
       	/**
       	 * those are samples
       	
       	 
       	 // to get single trip, for example the first trip record
       	 Move t0 = trips.get(0);
       	 
       	 // to get the start point of the move, same to get end point
       	 Stop s = t0.getStart();
       	 
       	 //to get the location of the start point
       	 double x = s.getX();
       	 
       	 //to get the distance of the move
       	 double dis = t0.getDis();
       	 
       	 //to get the start time of the move; getEndTime() to get the end time
       	 Date starttime = t0.getStartTime(); //
       	 
       	 //to get the hour of the time; same to get second, get mins
       	 int hour = starttime.getHours();
       	 int day = starttime.getday();
       	 
       	 // I think it is the same as what you did in the sever analysis, you can do it, and just spend more time on time.
       	 */
       	 
       	 
    
    }


 /*
    public void testing(){
    	
    	this.walkdis.add(10.0);
    	this.walkdis.add(60.0);
    	this.walkdis.add(90.0);
    	this.walkdis.add(30.0);
    	this.walkdis.add(40.0);
    	this.walkdis.add(60.0);
    	this.walkdis.add(10.0);
    	
    	this.busdis.add(10.0);
    	this.busdis.add(34.0);
    	this.busdis.add(23.0);
    	this.busdis.add(15.0);
    	this.busdis.add(17.0);
    	this.busdis.add(89.0);
    	this.busdis.add(30.0);
    	
    	this.cardis.add(20.0);
    	this.cardis.add(79.0);
    	this.cardis.add(40.0);
    	this.cardis.add(28.0);
    	this.cardis.add(19.0);
    	this.cardis.add(43.0);
    	this.cardis.add(89.0);


    	
    }*/

	public void reload() {
		// TODO Auto-generated method stub
		this.getDataFromFile();
		this.calculation();
	}
	
	//to clean every array
	public void cleanArray(){
		//before we have to clean up everything
       	this.busdis.clear();
       	this.busCarbon.clear();
       	this.carCarbon.clear();
       	this.cardis.clear();
       	this.totalCarbon.clear();
       	this.totalDis.clear();
       	this.trainCarbon.clear();
       	this.traindis.clear();
       	this.walkCarbon.clear();
       	this.walkdis.clear();
       
	}
	
	
	public void setMaxDis(double d){
		this.maximumDis = d;
	}
	
	public void setMinCarbon(double d){
		this.maximumCarbon = d;
	}
	
	public double getMaxDis(){
		return this.maximumDis;
	}
	
	public double getMaxCarbon(){
		return this.maximumCarbon;
	}

	public void setmyStartTime(double d){
		this.startTime = d;
	}
	
	public void setmyEndTime(double d){
		this.endTime = d;
	}
	
	public double getmyStartTime(){
		return this.startTime;
	}
	
	public double getmyEndTime(){
		return this.endTime;
	} 
}

